package schoolbolg_gh326.Adapter;

import java.util.ArrayList;
import java.util.List;

import schoolbolg_gh326.Bean.BlogBean;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class BlogDBAdapter {
	final static int VERSON = 1;
	private final static String DBNAME = "blog.db";
	private final static String TABLE = "blog";
	private final static String ID="_id";
	private final static String NAME="name";
	private final static String TEL="tel";
	
	
	private Context context;
	private OpenHelper openHelper;
	private SQLiteDatabase database;
	private static BlogDBAdapter dbAdapter;
	
	
	public static BlogDBAdapter getInstance(Context context){
		if(dbAdapter==null){
			dbAdapter=new BlogDBAdapter(context);
			dbAdapter.openDB();
		}
		return dbAdapter;
	}


	private BlogDBAdapter(Context context) {
		this.context = context;
		openHelper = new OpenHelper(context);
	}

	public void openDB() {
		
		if(database== null){
			database = openHelper.getWritableDatabase();
		}
	}

	public void closeDB() {
	
		if (database != null) {
			database.close();
			database = null;
		}
	}

	public long insert(BlogBean blogBean) {
		long id=-1 ;
		
		ContentValues values=new ContentValues();
		values.put(NAME, blogBean.getName());
		values.put(TEL, blogBean.getTel());
		id=database.insert(TABLE, null, values);
		return id;
	}
	
	public void update(BlogBean blogBean){
	
		ContentValues values=new ContentValues();
		values.put(NAME, blogBean.getName());
		values.put(TEL, blogBean.getTel());
		String[] whereArgs={String.valueOf(blogBean.get_id())};
		int cnt=database.update(TABLE, values,
				ID+"=?", whereArgs);
	}
	
	public int delete(BlogBean blogBean){
		int cnt=0;
	
		return cnt;
	}
	
	public int deleteById(int _id){
		int cnt=0;
	
		return cnt;
	}

	public void deleteAll(){
	
	}
	

	
	public List<BlogBean> queryAll(){
		Cursor cursor;
		
		cursor=database.query(TABLE, null, null, null, null, null, null);
		return convertToBlogBean(cursor);
	}
	
	public List<BlogBean> convertToBlogBean(Cursor cursor){
		List<BlogBean> blogBeans=new ArrayList<BlogBean>();
		
	
		
		if (cursor!=null) {
			int cnt=cursor.getCount();
			cursor.moveToFirst();
			for (int i = 0; i < cnt; i++) {
				BlogBean blogBean=new BlogBean();
				blogBean.set_id(cursor.getInt(cursor.getColumnIndex(ID)));
				blogBean.setName(cursor.getString(
						cursor.getColumnIndex(NAME)));
				blogBean.setTel(cursor.getString(cursor.getColumnIndex(TEL)));
				blogBeans.add(blogBean);
				cursor.moveToNext();
			}
		}
		
		return blogBeans;
	}
	
	public class OpenHelper extends SQLiteOpenHelper {

		public OpenHelper(Context context) {
			super(context, DBNAME, null, VERSON);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			String sql = "create table blog( _id integer primary key autoincrement, name varchar(64), tel varchar(64), type varchar(64))";
			db.execSQL(sql);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub

		}

	}
}
